#!/usr/bin/env python3
"""
merge_csv.py
将当前目录下所有 CSV 文件合并为一个文件，按文件名排序后合并
用法: python merge_csv.py input_dir output.csv
"""
import sys
import os
import pandas as pd
from glob import glob

def merge_csv(input_dir, output_file):
	csv_files = sorted(glob(os.path.join(input_dir, "*.csv")))
	ifnot csv_files:
		print("未找到 CSV 文件。")
		return
	df_list = [pd.read_csv(f) for f in csv_files]
	merged_df = pd.concat(df_list, ignore_index=True)
	merged_df.to_csv(output_file, index=False)
	print(f"已合并 {len(csv_files)} 个文件，结果保存为 {output_file}")

if __name__ == "__main__":
	if len(sys.argv) != 3:
		print("用法: python merge_csv.py input_dir output.csv")
		sys.exit(1)
	merge_csv(sys.argv[1], sys.argv[2])